package com.jvtd.eyl.ui.main.home.search;

import android.content.Context;
import android.content.Intent;
import android.databinding.DataBindingUtil;
import android.os.Handler;
import android.text.TextUtils;
import android.view.KeyEvent;
import android.view.View;
import android.view.WindowManager;
import android.view.inputmethod.EditorInfo;
import android.view.inputmethod.InputMethodManager;
import android.widget.TextView;

import com.jvtd.eyl.R;
import com.jvtd.eyl.app.MyApplication;
import com.jvtd.eyl.base.BaseMvpActivity;
import com.jvtd.eyl.bean.binding.PeriodicalBean;
import com.jvtd.eyl.databinding.ActivitySearchBinding;
import com.jvtd.utils.KeyboardUtils;
import com.jvtd.widget.editText.JvtdRightClickEditText;

import org.litepal.util.LogUtil;

import java.util.List;
import java.util.Timer;
import java.util.TimerTask;

import javax.inject.Inject;

/**
 * 搜索期刊
 * <p>
 * 作者:chenlei
 * 时间:2018/11/14 5:33 PM
 */
public class SearchActivity extends BaseMvpActivity implements SearchMvpView, JvtdRightClickEditText.OnRightClickListener {
    @Inject
    SearchPresenter<SearchMvpView> mPresenter;
    private ActivitySearchBinding mBinding;

    public static Intent getIntent(Context context) {
        return new Intent(context, SearchActivity.class);
    }

    @Override
    protected void initInject() {
        getActivityComponent().inject(this);
    }

    @Override
    protected void getLayout() {
        mBinding = DataBindingUtil.setContentView(this, R.layout.activity_search);
    }

    @Override
    protected void onDestroy() {
        mPresenter.onDetach();
        super.onDestroy();
    }

    @Override
    protected void initViewAndData() {
        mPresenter.onAttach(this);

        initToolbar();

        initRecyclerView();
    }

    private void initToolbar() {
        setToolbar(mBinding.toolBar.toolBar, true);
        mBinding.toolBar.toolbarSearchEditText.setOnRightClickListener(this);
        mBinding.toolBar.toolbarSearchEditText.setHint("");
        //搜索键方法监听
        mBinding.toolBar.toolbarSearchEditText.setOnEditorActionListener((v, actionId, event) -> {
            if (actionId == EditorInfo.IME_ACTION_SEARCH) {//搜索按键action
                if (TextUtils.isEmpty(mBinding.toolBar.toolbarSearchEditText.getText())) {
                    return true;
                }
                onClickRight(null);
                return true;
            }
            return false;
        });
        //唤起键盘 加入延时 不延时可能造成唤起失败
        new Handler().postDelayed(() -> {
            if (mBinding.toolBar.toolbarSearchEditText != null || mContext != null){
                KeyboardUtils.showSoftInput(mBinding.toolBar.toolbarSearchEditText, mContext);
            }
        },200);
//    mEditText.setText("测试");
    }

    private void initRecyclerView() {
        mBinding.recyclerView.openEmptyView(null);
    }

    @Override
    public void searchPeriodicalListSuccess(List<PeriodicalBean> periodicalBeans) {
        mBinding.recyclerView.setData(periodicalBeans);
    }

    @Override
    public void searchPeriodicalListFailed() {
        mBinding.recyclerView.firstLoadFailed();
    }

    @Override
    public void onClickRight(View view) {
        hideKeyboard();
        mPresenter.searchPeriodicalList(mBinding.toolBar.toolbarSearchEditText.getText().toString().trim());
    }
}
